// This template will be used for displaying the MCQ to the student.
// The window will display a question, 4 options and a hint. Student 
// should be able to pick one or more options and submit.
// author vinil.bhandari@kaplan.com

var win = Titanium.UI.currentWindow;
win.barColor = 'null';

win.backgroundColor = '#B6CCDD';
var whiteBackground = Ti.UI.createView({ width :'100%', height : 63, top : -4, zIndex : 1, backgroundColor: 'white',borderWidth:2,borderColor:'#aaa' }); 
var Logo = Ti.UI.createImageView({ width :'100%', height : 55, top : 0,  zIndex : 1, image : '../images/dashboard/kaptest.png' }); 

win.add(Logo);
win.add(whiteBackground);

var maxRow = 0;
var answer='';
if(win.info==null)
var questionNmbr=1;
else
var questionNmbr=win.info;

if (Titanium.Platform.name == 'iPhone OS'){
	var view2 = Ti.UI.createView({
	backgroundColor:'#B6CCDD'
});
win.add(view2);

var visible = false;

	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});


	var b9 = Titanium.UI.createButton({
		title:'Submit',
		style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED,
		color:'#463E77',
		backgroundColor:'#463E77'
	});

	b9.addEventListener('click', function()
	{
	var newWindow = Titanium.UI.createWindow({  
    title:'Practice',
    url: 'testTemplate.js',
    info: questionNmbr+1
});
newWindow.animate({view:view2, transition:Ti.UI.iPhone.AnimationStyle.CURL_DOWN});
Titanium.UI.currentTab.open(newWindow,{animated:true});
		
	});

	win.setToolbar([flexSpace,flexSpace,b9],{translucent:true});
}


//Get DB object
var dbase = Titanium.Database.open('gyaan');
// Create a TableView row for each question in the database table
var myResultSet = dbase.execute('SELECT * FROM options WHERE  q_id='+questionNmbr);
var myResultSetQ = dbase.execute('SELECT * FROM questions WHERE  id='+questionNmbr);


//Mike testing
var inputData1 = [];

inputData1[0] = Ti.UI.createTableViewRow({hasChild:false,height:'auto'});
inputData1[1] = Ti.UI.createTableViewRow({hasChild:false,height:'auto'});
inputData1[2] = Ti.UI.createTableViewRow({hasChild:false,height:'auto'});
inputData1[3] = Ti.UI.createTableViewRow({hasChild:false,height:'auto'});
inputData1[4] = Ti.UI.createTableViewRow({hasChild:false,height:'auto'});

function addRow(idx,text)
{
	inputData1[idx].add(Ti.UI.createLabel({
		text:text,
		height:'auto',
		width:'auto'
	}));
}
while (myResultSet.isValidRow()) {
	maxRow=1;
	
addRow(0,'A. '+myResultSet.fieldByName('a'));
addRow(1,'B. '+myResultSet.fieldByName('b'));
addRow(2,'C. '+myResultSet.fieldByName('c'));
addRow(3,'D. '+myResultSet.fieldByName('d'));
addRow(4,'E. '+myResultSet.fieldByName('e'));

myResultSet.next();
}

// Mike tested


if(myResultSetQ.isValidRow()){
	
var tableView1 = Titanium.UI.createTableView({
    data:inputData1,
    bottom: 60,
    style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
    minRowHeight:80,
    top:70,
    headerTitle:myResultSetQ.fieldByName('title')
	
    
});	
var flag1=true;
var flag2=true;
var flag3=true;
var flag4=true;
var flag5=true;

tableView1.addEventListener('click', function(e){
    var index = e.index;
    var section = e.section;
    var dbaseNew = Titanium.Database.open('gyaan');
    dbaseNew.execute('UPDATE questions SET answer_provided="'+index+ '" WHERE id='+questionNmbr);
    if(!flag1 || !flag2 || !flag3 || !flag4 || !flag5){
    	//var rating = !flag1?1:!flag2?2:!flag3?3:!flag4?4:!flag5?5:0;
    	//dbaseNew.execute('INSERT INTO q_stats (id , q_id , ratings , user_id , share_count ) VALUES(?, ?, ?, ?, ?)', null,  questionNmbr, rating ,'','');
    	
    }
    dbaseNew.close();
    
    setTimeout(function()
	{
		// reset checks
		for (var i=0;i<section.rows.length;i++)
		{
			section.rows[i].hasCheck = false;
			if (Titanium.Platform.name == 'iPhone OS'){
				section.rows[i].children[0].color = 'black';
			}
		}
		// set current check
		section.rows[index].hasCheck = true;
		if (Titanium.Platform.name == 'iPhone OS'){
			section.rows[index].children[0].color = 'purple';
		}
		
	},250);
       
});
}
if(maxRow==1){
	win.add(tableView1);
}else
{
	myResultSet.close();
	myResultSetQ.close();
	var a = Titanium.UI.createAlertDialog({
	title:'Congrats. You finished your test'
});
a.show();
		var newWindow = Titanium.UI.createWindow({  
    title:'Results',
    url: 'result.js',
    });
	Titanium.UI.currentTab.open(newWindow);

}
myResultSet.close();
myResultSetQ.close();
dbase.close();

if (Titanium.Platform.name != 'iPhone OS'){

	var quizButton = Titanium.UI.createButton({
		color:'#fff',
		bottom:20,
		width:301,
		height:57,
		font:{fontSize:20,fontWeight:'bold',fontFamily:'Helvetica Neue'},
		title:'Submit'
	});
	win.add(quizButton);
	quizButton.addEventListener('click', function(){
	var newWindow = Titanium.UI.createWindow({  
    title:'Practice',
    url: 'testTemplate.js',
    info: questionNmbr+1
	});

Titanium.UI.currentTab.open(newWindow);
});

}



var imageView1 = Titanium.UI.createView({ width: 30, height: 30, bottom:40, left: 10, backgroundImage: '../images/star_2.png' });
var imageView2 = Titanium.UI.createView({ width: 30, height: 30, bottom:40, left: 55, backgroundImage: '../images/star_2.png' });
var imageView3 = Titanium.UI.createView({ width: 30, height: 30, bottom:40, left: 100, backgroundImage: '../images/star_2.png' });
var imageView4 = Titanium.UI.createView({ width: 30, height: 30, bottom:40, left: 145, backgroundImage: '../images/star_2.png' });
var imageView5 = Titanium.UI.createView({ width: 30, height: 30, bottom:40, left: 190, backgroundImage: '../images/star_2.png' });
imageView1.addEventListener('click', function (e){
  if (flag1) {
    imageView1.backgroundImage = '../images/star_1.png';
    flag1=false;
  } else {
    imageView1.backgroundImage = '../images/star_2.png';
    imageView2.backgroundImage = '../images/star_2.png';
    imageView3.backgroundImage = '../images/star_2.png';
    imageView4.backgroundImage = '../images/star_2.png';
    imageView5.backgroundImage = '../images/star_2.png';
    flag1=true;
    flag2=true;
    flag3=true;
    flag4=true;
    flag5=true;
  }
  Ti.API.info('imageView.image = ' + imageView1.backgroundImage);
});

imageView2.addEventListener('click', function (e){
  if (flag2) {
    imageView2.backgroundImage = '../images/star_1.png';
    imageView1.backgroundImage = '../images/star_1.png';
    flag2=false;
  } else {
    imageView2.backgroundImage = '../images/star_2.png';
    imageView3.backgroundImage = '../images/star_2.png';
    imageView4.backgroundImage = '../images/star_2.png';
    imageView5.backgroundImage = '../images/star_2.png';
    flag2=true;
    flag3=true;
    flag4=true;
    flag5=true;
  }
 
});

imageView3.addEventListener('click', function (e){
  if (flag3) {
    imageView3.backgroundImage = '../images/star_1.png';
    imageView2.backgroundImage = '../images/star_1.png';
    imageView1.backgroundImage = '../images/star_1.png';
    flag3=false;
  } else {
    imageView3.backgroundImage = '../images/star_2.png';
    imageView4.backgroundImage = '../images/star_2.png';
    imageView5.backgroundImage = '../images/star_2.png';
    flag3=true;
    flag4=true;
    flag5=true;
  }
 
});

imageView4.addEventListener('click', function (e){
  if (flag4) {
    imageView4.backgroundImage = '../images/star_1.png';
    flag4=false;
    imageView3.backgroundImage = '../images/star_1.png';
    imageView2.backgroundImage = '../images/star_1.png';
    imageView1.backgroundImage = '../images/star_1.png';
  } else {
    imageView4.backgroundImage = '../images/star_2.png';
    imageView5.backgroundImage = '../images/star_2.png';
    flag4=true;
    flag5=true;
  }
  
});

imageView5.addEventListener('click', function (e){
  if (flag5) {
    imageView5.backgroundImage = '../images/star_1.png';
    imageView4.backgroundImage = '../images/star_1.png';
    imageView3.backgroundImage = '../images/star_1.png';
    imageView2.backgroundImage = '../images/star_1.png';
    imageView1.backgroundImage = '../images/star_1.png';
    flag5=false;
  } else {
    imageView5.backgroundImage = '../images/star_2.png';
    flag5=true;
  }
  
});


win.add(imageView1);
win.add(imageView2);
win.add(imageView3);
win.add(imageView4);
win.add(imageView5);




var number = Titanium.UI.createLabel({
        text:"# "+questionNmbr,
        height:30,
        right:10,
        font : {fontSize:30,fontFamily:'Marker Felt', fontWeight:'bold'},
         top : 15,
         zIndex : 10,
         textAlign:'right',
        color:"#000000"
    });
win.add(number);



var record = Ti.UI.createButton({
	backgroundImage:'../images/dashboard/movie_off.png',
	height:33,
	width:33
});
win.rightNavButton = record;

var pic = Ti.UI.createButton({
	backgroundImage:'../images/camera.png',
	height:33,
	width:33,
	top:15,
	left:10,
	zIndex:3,
});
win.add(pic);

record.addEventListener('click', function (e){
	if(!Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'mymovie'+questionNmbr+'.mov').exists()){
		alert("Sorry. There is no Video asset for this question");
		return;
	}
	Titanium.API.info("Play Video");
	
	
	b9=null;
		var b10 = Titanium.UI.createButton({
		title:'Back',
		height:33,
		left:1,
		width:150,
		top:80,
		zIndex:3
	});
	win.add(b10);

  		var activeMovie = Titanium.Media.createVideoPlayer({
			backgroundColor:'#111',
			movieControlMode:Titanium.Media.VIDEO_CONTROL_DEFAULT,
			scalingMode:Titanium.Media.VIDEO_SCALING_ASPECT_FILL,
			//contentURL:movieFile.nativePath
			media:Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'mymovie'+questionNmbr+'.mov')
		});
		activeMovie.play();
		
		if (parseFloat(Titanium.Platform.version) >= 3.2)
		{
			win.add(activeMovie);
		}
		
	b10.addEventListener('click', function()
	{
		var newWindow = Titanium.UI.createWindow({  
	    title:'Practice',
	    url: 'testTemplate.js',
	    info: questionNmbr
	});
	newWindow.animate({view:view2, transition:Ti.UI.iPhone.AnimationStyle.CURL_DOWN});
	Titanium.UI.currentTab.open(newWindow,{animated:true});
	});
	
 
});

pic.addEventListener('click', function (e){
	Ti.Media.openPhotoGallery({
	 
	    success:function(event)
	    {
	        Ti.API.info('success'+e.media);
	    },
	    cancel:function()
	    {
	        Ti.API.info('...canceled');
	    },
	    error:function(error)
	    {
	        Ti.API.info('error');
	    },
	    allowImageEditing:true
	});
 
});

var share = Ti.UI.createImageView({ width:45, height:45, top : 63, right:1, zIndex : 1, image : '../images/share.png' }); 
win.add(share);
